/**
 * Created by no1 on 2015/8/28.
 */
'use strict';
var mysql = require('../mysql.js');
var C = require('../../config/index');
var F = require('../../common/function');
var MPosts = require('../../model/posts.js');
var MTags = require('../../model/tags.js');
var MUser = require('../../model/users.js');
var Mman = require('../../common/man.js');
var imgRule = require('../../common/imgRule.js');
var _ = require('underscore');
var _s = require('underscore.string');
var md5 = require('md5');
var request = require('koa-request');
var async_request = require('request');
var logstash = require('./../logFlow/logstash.js');
_.v = require('validator');


/*
 *@todo API38	主题帖分享后回调统计接口
 * */
exports.shareCallback = function*(Env) {
  var fields = Env.I;
  var act = fields.act;
  var type = parseInt(fields.type) > 0 ? parseInt(fields.type) : 0;  // 1 微信好友 2 微信朋友圈 3 QQ好友 4 QQ空间
  if (!_.v.isIn(type, [0, 1, 2, 3, 4])) {
    return F.returnMsg(400, '传递的参数错误', 1);
  }

  var tid = fields.tid;
  var tagId = fields.tagid;
  var share_uid = fields.uid;
  var app = fields.app;

  if (tid) {//分享帖子
    // 判断帖子是否存在
    var isThreadExist = yield MPosts.isThreadExist(tid);
    if (!isThreadExist) {
      return F.returnMsg(300, '帖子不存在', 3);
    }
  } else if (tagId) {//分享标签
    var tagData = yield mysql.query('SELECT tagid FROM mh_tags WHERE tagid=?', [tagId]);
    if (tagData.length == 0) {
      return F.returnMsg(300, '标签不存在', 3);
    }
  } else if (share_uid) {//分享标签
    var userData = yield mysql.query('SELECT uid FROM mh_user_info WHERE uid=?', [share_uid]);
    if (userData.length == 0) {
      return F.returnMsg(300, '用户不存在', 3);
    }
  } else if (app) {
    if (app != 1) {
      return F.returnMsg(400, '传递的参数错误', 3);
    }
  }

  if (act == 1) { // 记录分享资料
    let fields = Env.I;
    let loginStatus = fields.loginStatus;
    //var loginStatus = yield F.isLogin(Env);
    var uid;
    if (!loginStatus.status) { // 未登录
      uid = 0;
    } else { // 登录
      uid = loginStatus.uid;
    }
    var gain = 0;
    if (tid) {//分享帖子
      // 此主题帖分享次数+1
      mysql.query('UPDATE mh_thread SET sharetimes=sharetimes+1 WHERE tid=?', [tid]);
      //更新帖子信息开始
      var key = C.redisPre.thread_info;
      key = _s.sprintf(key, parseInt(tid));
      var temp = yield F.redisCo.EXISTS(key);
      if (temp == 1) {
        F.redisClient.HINCRBY(key, 'sharetimes', 1);//帖子被分享数+1
      }
      //更新帖子信息结束

      if (loginStatus.status) {
        // 此用户分享帖子次数+1
        mysql.query('UPDATE mh_user_info SET sharetimes=sharetimes+1 WHERE uid=?', [uid]);
        //更新分享人信息开始
        key = C.redisPre.user_info;
        key = _s.sprintf(key, parseInt(uid));
        var temp = yield F.redisCo.EXISTS(key);
        if (temp == 1) {
          F.redisClient.HINCRBY(key, 'sharetimes', 1);//用户分享主题帖数+1
        }
        //更新分享人信息结束
      }
      mysql.query('INSERT INTO mh_share_log(tid,uid,type,timeline) VALUES(?,?,?,UNIX_TIMESTAMP())', [tid, uid, type]);
      if (uid > 0) {
        gain = yield F.generateIntegralLog(uid, 19, tid);//分享帖子成功后获得积分
      }
    } else if (tagId) {
      // 此标签分享次数+1
      mysql.query('UPDATE mh_tags SET sharetimes=sharetimes+1 WHERE tagid=?', [tagId]);
      if (loginStatus.status) {
        // 此用户分享标签次数+1
        mysql.query('UPDATE mh_user_info SET sharetagtimes=sharetagtimes+1 WHERE uid=?', [uid]);
        //更新分享人信息开始
        //key=C.redisPre.user_info;
        //key=_s.sprintf(key, parseInt(uid));
        //var temp=yield F.redisCo.EXISTS(key);
        //if(temp==1){
        //  F.redisClient.HINCRBY(key,'sharetimes',1);//用户分享主题帖数+1
        //}
        //更新分享人信息结束
      }
      mysql.query('INSERT INTO mh_share_log(tagid,uid,type,timeline) VALUES(?,?,?,UNIX_TIMESTAMP())', [tagId, uid, type]);
      if (uid > 0) {
        gain = yield F.generateIntegralLog(uid, 20, tagId);//分享标签成功后获得积分
      }
    } else if (share_uid) {
      // 此人的被分享次数+1
      mysql.query('UPDATE mh_user_info SET beshared=beshared+1 WHERE uid=?', [share_uid]);
      if (loginStatus.status) {
        // 此用户分享他人个人详情次数+1
        mysql.query('UPDATE mh_user_info SET shareusertimes=shareusertimes+1 WHERE uid=?', [uid]);
      }
      mysql.query('INSERT INTO mh_share_log(uid,userid,type,timeline) VALUES(?,?,?,UNIX_TIMESTAMP())', [uid, share_uid, type]);
      if (uid > 0) {
        gain = yield F.generateIntegralLog(uid, 21);//分享某用户成功后获得积分
      }
    } else if (app) {
      if (loginStatus.status) {
        // 此用户分享app次数+1
        mysql.query('UPDATE mh_user_info SET shareapptimes=shareapptimes+1 WHERE uid=?', [uid]);
      }
      mysql.query('INSERT INTO mh_share_log(app,uid,type,timeline) VALUES(?,?,?,UNIX_TIMESTAMP())', [app, uid, type]);
      if (uid > 0) {
        gain = yield F.generateIntegralLog(uid, 18);//分享标签成功后获得积分
      }
    }
    if (uid > 0) {
      return F.returnMsg(200, {act: '分享成功', e: gain}, 4);
    }
    return F.returnMsg(200, '分享成功', 3, {tid: tid});
  } else {
    return F.returnMsg(400, '传递的参数错误', 1);
  }
};
